package org.dromara.student.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.web.enums.AppConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.dromara.student.domain.bo.XjStudentTaskBo;
import org.dromara.student.domain.vo.XjStudentTaskVo;
import org.dromara.student.domain.XjStudentTask;
import org.dromara.student.mapper.XjStudentTaskMapper;
import org.dromara.student.service.IXjStudentTaskService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 学生作业Service业务层处理
 *
 * @author Lion Li
 * @date 2024-12-17
 */
@RequiredArgsConstructor
@Service
public class XjStudentTaskServiceImpl implements IXjStudentTaskService {

    private final XjStudentTaskMapper baseMapper;

    /**
     * 查询学生作业
     *
     * @param taskId 主键
     * @return 学生作业
     */
    @Override
    public XjStudentTaskVo queryById(String taskId){
        return baseMapper.selectVoById(taskId);
    }

    /**
     * 分页查询学生作业列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 学生作业分页列表
     */
    @Override
    public TableDataInfo<XjStudentTaskVo> queryPageList(XjStudentTaskBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<XjStudentTask> lqw = buildQueryWrapper(bo);
        Page<XjStudentTaskVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的学生作业列表
     *
     * @param bo 查询条件
     * @return 学生作业列表
     */
    @Override
    public List<XjStudentTaskVo> queryList(XjStudentTaskBo bo) {
        LambdaQueryWrapper<XjStudentTask> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<XjStudentTask> buildQueryWrapper(XjStudentTaskBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<XjStudentTask> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTaskName()), XjStudentTask::getTaskName, bo.getTaskName());
        lqw.eq(bo.getParentId() != null, XjStudentTask::getParentId, bo.getParentId());
        lqw.eq(bo.getAiScore() != null, XjStudentTask::getAiScore, bo.getAiScore());
        lqw.eq(bo.getTeacherScore() != null, XjStudentTask::getTeacherScore, bo.getTeacherScore());
        lqw.eq(bo.getTotalScore() != null, XjStudentTask::getTotalScore, bo.getTotalScore());
        lqw.eq(StringUtils.isNotBlank(bo.getAiSuggestion()), XjStudentTask::getAiSuggestion, bo.getAiSuggestion());
        lqw.eq(StringUtils.isNotBlank(bo.getTeacherSuggestion()), XjStudentTask::getTeacherSuggestion, bo.getTeacherSuggestion());
        lqw.eq(StringUtils.isNotBlank(bo.getGlipJson()), XjStudentTask::getGlipJson, bo.getGlipJson());
        return lqw;
    }

    /**
     * 新增学生作业
     *
     * @param bo 学生作业
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(XjStudentTaskBo bo) {
        XjStudentTask add = MapstructUtils.convert(bo, XjStudentTask.class);
        validEntityBeforeSave(add);
        // 设置删除标记
        if (add != null) {
            add.setDelFlag(AppConstants.DEL_FLAG.NORMAL);
        }
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTaskId(add.getTaskId());
        }
        return flag;
    }

    /**
     * 修改学生作业
     *
     * @param bo 学生作业
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(XjStudentTaskBo bo) {
        XjStudentTask update = MapstructUtils.convert(bo, XjStudentTask.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(XjStudentTask entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除学生作业信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
